import { _decorator, EventTouch, Label, Node, TiledMap, tween, Tween, UITransform, Vec2, Vec3, view } from 'cc';
import { PanelBase } from '../../Basic/UI/PanelBase';
import { LayerType, SoundCode } from '../../Basic/Config/BaseEnum';
import { MouseCtrl } from '../Game/MouseCtrl';
import { Tool } from '../../Util/Tool';
import { CallBackHandler } from '../../Basic/Model/PoolObj/CallBackHandler';
import { LevelModel } from './LevelModel';
import { PoolFactory } from '../../Basic/Component/PoolFactory';
import { DataMgr, GameData, PlayerData } from '../../Basic/Manager/DataManager';
import { GameParams, PlayerParams } from '../../Basic/Config/DataConfig';
import { GameModel } from '../Game/GameModel';
import { RankModel } from '../Dialog/RankModel';
import { AudioManager } from '../../Basic/Manager/AudioManager';
import { SettingModel } from '../Setting/SettingModel';
import { HandbookModel } from '../Handbook/HandbookModel';
import { GravityModel, GEEvent } from '../../Basic/Model/SDK/GravityModel';
import { autoComp, rootNode } from '../../Basic/Manager/BaseManager';
import { SidebarModel_Ins } from '../Sidebar/SidebarModel';
const { ccclass, property } = _decorator;

@ccclass('LevelPanel')
export class LevelPanel extends PanelBase {
  panelLayer: LayerType.MenuLayer = LayerType.MenuLayer;

  @autoComp(PoolFactory, rootNode)
  poolFac: PoolFactory;
  pressPos: Vec2;
  protected onLoad(): void {
    // LevelModel.Ins().btnStart = this.btnStart;
    const time = Math.floor((Date.now() - LevelModel.Ins().loadTime) / 1000);
    // GravityModel.I.track(GEEvent.LOAD, { loadTime: time });

    // GravityModel.I.track(GEEvent.LOGIN, { isFirst: PlayerData.get(PlayerParams.isNovice) });
    if (PlayerData.get(PlayerParams.isNovice)) DataMgr.changePlayerAttr(PlayerParams.isNovice, false, true);
  }

  protected onEnable(): void {
    super.onEnable();

    AudioManager.Singleton.loadAndPlayMusic(SoundCode.BGM_MAIN);

    const gameLevel = GameData.get(GameParams.gameLevel);
    let levelId = Math.ceil(gameLevel / 5) * 100 + ((gameLevel - 1) % 5) + 1;
    const lastId = DataMgr.tables.TbLevel.getDataList()[DataMgr.tables.TbLevel.getDataList().length - 1].id;
    levelId = levelId >= lastId ? lastId : levelId;
    // this.lab_level.string = `${DataMgr.tables.TbLevel.get(levelId).name}`;

    // this.btnStart.active = false;
    // this.btnReset.active = false;
    // LevelModel.Ins().readMapPoint();
    // LevelModel.Ins().initLevelItems();
    // this.moveToLevel();
    // const tsc = CallBackHandler.pop();
    // const tmc = CallBackHandler.pop();
    // const tec = CallBackHandler.pop();
    // tsc.callBack = this.onTouchStart;
    // tmc.callBack = this.onTouchMove;
    // tec.callBack = this.onTouchEnd;
    // tsc.target = this;
    // tmc.target = this;
    // tec.target = this;
    // this.mouseCtrl.registEvent(tsc, tmc, tec);
  }

  // onTouchStart(t: EventTouch) {
  //   this.pressPos = t.getUILocation();
  // }

  // onTouchMove(t: EventTouch) {
  //   const tempPos = t.getUILocation();
  //   const space = Tool.Vec2Sub(tempPos, this.pressPos);

  //   const retPos = new Vec3();
  //   retPos.x = this.levelMap.node.getPosition().x + space.x;
  //   retPos.y = this.levelMap.node.getPosition().y + space.y;
  //   this.updateMapPos(retPos);
  //   this.pressPos = tempPos;
  // }

  // // eslint-disable-next-line @typescript-eslint/no-unused-vars
  // onTouchEnd(t: EventTouch) {
  //   this.pressPos = null;
  //   this.updateResetBtnState();
  // }

  // updateResetBtnState() {
  //   const curLevel = DataMgr.gameParams.get(GameParams.gameLevel) - 1;
  //   const item = LevelModel.Ins().levelItems[curLevel];
  //   const curPos = this.levelMap.node.getPosition().clone();
  //   const itemPos = item.node.getPosition().clone();
  //   const dir0 = new Vec2(-curPos.x, -curPos.y);
  //   const dir1 = Tool.Vec2Mut(new Vec2(itemPos.x, itemPos.y), this.levelMapTrans.node.scale.x);
  //   const space = Tool.Vec2Sub(dir0, dir1);
  //   this.btnReset.active = space.length() >= 200;
  // }

  // updateMapPos(retPos: Vec3, isTween: boolean = false) {
  //   const viewSize = view.getVisibleSize();

  //   if (retPos.x >= (this.levelMapTrans.width / 2) * this.levelMapTrans.node.scale.x - viewSize.width / 2) {
  //     retPos.x = (this.levelMapTrans.width / 2) * this.levelMapTrans.node.scale.x - viewSize.width / 2;
  //   } else if (retPos.x <= -(this.levelMapTrans.width / 2) * this.levelMapTrans.node.scale.x + viewSize.width / 2) {
  //     retPos.x = -(this.levelMapTrans.width / 2) * this.levelMapTrans.node.scale.x + viewSize.width / 2;
  //   }

  //   if (retPos.y >= (this.levelMapTrans.height / 2) * this.levelMapTrans.node.scale.y - viewSize.height / 2) {
  //     retPos.y = (this.levelMapTrans.height / 2) * this.levelMapTrans.node.scale.y - viewSize.height / 2;
  //   } else if (retPos.y <= -(this.levelMapTrans.height / 2) * this.levelMapTrans.node.scale.y + viewSize.height / 2) {
  //     retPos.y = -(this.levelMapTrans.height / 2) * this.levelMapTrans.node.scale.y + viewSize.height / 2;
  //   }

  //   Tween.stopAllByTarget(this.levelMap.node);
  //   if (isTween) {
  //     tween(this.levelMap.node).to(0.2, { position: retPos }).start();
  //   } else {
  //     this.levelMap.node.setPosition(retPos);
  //   }
  // }

  // moveToLevel(levelId: number = -1) {
  //   const curLevel = levelId == -1 ? DataMgr.gameParams.get(GameParams.gameLevel) - 1 : levelId;
  //   const item = LevelModel.Ins().levelItems[curLevel >= LevelModel.Ins().levelItems.length ? LevelModel.Ins().levelItems.length - 1 : curLevel];

  //   const curPos = this.levelMap.node.getPosition().clone();
  //   const itemPos = item.node.getPosition().clone();

  //   const dir0 = new Vec2(-curPos.x, -curPos.y);
  //   const dir1 = Tool.Vec2Mut(new Vec2(itemPos.x, itemPos.y), this.levelMapTrans.node.scale.x);

  //   const space = Tool.Vec2Sub(dir0, dir1);
  //   space.x += curPos.x;
  //   space.y += curPos.y;

  //   this.updateMapPos(new Vec3(space.x, space.y), true);
  // }

  // onClickReset() {
  //   this.moveToLevel();
  //   this.btnReset.active = false;
  // }

  // onClickStart() {
  //   if (DataMgr.gameParams.get(GameParams.energy_award) >= 3) {
  //     this.closeSelf();
  //     GameModel.Ins().Show();
  //   } else if (DataMgr.gameParams.get(GameParams.energy) <= 0) {
  //     EnergyModel.Ins().Show();
  //   } else {
  //     EnergyModel.Ins().useEnergy(1);
  //     this.closeSelf();
  //     GameModel.Ins().Show();
  //   }
  // }

  onClickBtn(t: Touch, data) {
    const key = data.toString();
    AudioManager.Singleton.playSound(SoundCode.BTN_CLICK);
    switch (key) {
      case 'start':
        // if (DataMgr.gameParams.get(GameParams.energy_award) >= 3) {
        this.closeSelf();
        GameModel.Ins().Show();
        // } else if (DataMgr.gameParams.get(GameParams.energy) <= 0) {
        //   EnergyModel.Ins().Show();
        // } else {
        //   EnergyModel.Ins().useEnergy(1);
        //   this.closeSelf();
        //   GameModel.Ins().Show();
        // }
        break;
      case 'reset':
        // this.moveToLevel();
        // this.btnReset.active = false;
        break;
      case 'rank':
        RankModel.Ins().Show();
        break;
      case 'setting':
        SettingModel.Ins().Show();
        break;
      case 'handbook':
        HandbookModel.Ins().Show();
        break;
      case 'sidebar':
        SidebarModel_Ins.Show();
        break;
    }
  }
}
